#region File Description
//-----------------------------------------------------------------------------
// Projectile.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion



namespace ShipGame
{
    public class Projectile
    {
        ProjectileType projectileType;  // projectile type

        RenderTechnique technique;      // render technique

        Model model;                    // projectile model
        int player;                     // player owning the projectile

        Vector3 sourcePosition;         // source position
        Vector3 destinationPosition;    // destination position

        float elapsedTime;            // elapsed time since created
        float totalTime;              // total time to reach destination

        Matrix transform;             // current projectile transform matrix
        
        float contactDamage;          // contact damage if projectile hits a player

        AnimSpriteType animatedSprite;    // animated sprite to play when projectile hits
        float animatedSpriteSize;         // size of explosion animated sprite
        float animatedSpriteFrameRate;    // framerate for explosion animated sprite
        DrawMode animatedSpriteDrawMode;  // animated sprite drawing mode
        float explosionDamage;        // splash damage for explosion
        float explosionDamageRadius;  // splash damage radius
        String explosionSound;        // explosion sound

        ParticleSystem system;        // particle system used for projectile trail
        Matrix systemTransform;       // local transform to position particle system

        /// <summary>
        /// Create a new projectile
        /// </summary>
        public Projectile(
                ProjectileType type, Model model, 
                int player, float velocity, float damage, 
                Matrix source, Vector3 destination, 
                RenderTechnique technique)
        {
            projectileType = type;
            this.model = model;
            this.player = player;

            sourcePosition = source.Translation;
            destinationPosition = destination;

            this.technique = technique;

            contactDamage = damage;

            elapsedTime = 0;
            totalTime = (source.Translation - destination).Length() / velocity;
            transform = source;
        }

        /// <summary>
        /// Set projectile explosion parameters
        /// </summary>
        public void SetExplosion(
                AnimSpriteType sprite, float size, float frameRate, DrawMode mode,
                float damage, float damageRadius, String sound)
        {
            animatedSprite = sprite;
            animatedSpriteSize = size;
            animatedSpriteFrameRate = frameRate;
            animatedSpriteDrawMode = mode;
            explosionDamage = damage;
            explosionDamageRadius = damageRadius;
            explosionSound = sound;
        }

        /// <summary>
        /// Set projectile trail parameters
        /// </summary>
        public void SetTrail(ParticleSystem trail, Matrix transform)
        {
            system = trail;
            systemTransform = transform;
        }

        /// <summary>
        /// Update projectile
        /// </summary>
        

        /// <summary>
        /// Draw projectile
        /// </summary>
        public void Draw(GameManager game, GraphicsDevice gd,
            RenderTechnique defaultTechnique, Vector3 cameraPosition, 
            Matrix viewProjection, LightList lights)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            if (technique == RenderTechnique.ViewMapping)
            {
                game.DrawModel(gd, model, technique,
                        cameraPosition, transform, viewProjection, null);
            }
            else
            {
                game.DrawModel(gd, model, defaultTechnique,
                        cameraPosition, transform, viewProjection, lights);
            }
        }
    }
}
